package jok.thread;

public class Test {

	public static void main(String[] args) {

		// newThread();

		// newThread2();

		// testPriority();

		// testJoin();

		// testYield();

		testSetDaemon();

		// testDeadlockThread();
	}

	public static void newThread() {
		// 线程实现：继承方式
		AThread at = new AThread("线程1");
		at.start();
	}

	public static void newThread2() {
		// 线程实现：接口实现方式
		BThread bt = new BThread();
		Thread t = new Thread(bt, "线程2");
		t.start();
	}

	public static void testPriority() {
		AThread at1 = new AThread();
		AThread at2 = new AThread();
		AThread at3 = new AThread();

		at3.setPriority(1);// 1-9,默认5
		at1.setPriority(9);

		at1.start();
		at2.start();
		at3.start();
	}

	public static void testJoin() {
		AThread at = new AThread();
		at.start();
		for (int i = 0; i < 10; i++) {
			if (i == 3) {
				try {
					at.join();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			System.out.println("主线程：HelloWorld " + i);
		}
	}

	public static void testYield() {
		AThread at = new AThread();
		YieldThread yt = new YieldThread();
		at.start();
		yt.start();
	}

	public static void testSetDaemon() {
		AThread at = new AThread();
		at.setDaemon(true);
		at.start();
		for (int i = 0; i < 10; i++) {
			System.out.println("主线程：HelloWorld " + i);
			try {
				Thread.sleep(5);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	public static void testDeadlockThread() {
		Thread t1 = new Thread(new DeadlockThread(true));
		Thread t2 = new Thread(new DeadlockThread(false));

		t1.start();
		t2.start();
	}
}
